package edu.cs.networkmonitor.devices;

import java.util.Date;
import javax.print.PrintService;
import javax.print.attribute.Attribute;
import javax.print.attribute.AttributeSet;

public class DeviceSource extends BaseSource {

	public enum DEVICETYPE {
		PRINTER
	}

	private PrintService _printService;
	private DEVICETYPE _deviceType;

	public DeviceSource(PrintService ps) {
		_printService = ps;
		_sourceName = ps.getName();
		_deviceType = DEVICETYPE.PRINTER;
	}

	@Override
	public boolean connect() {
		boolean status = false;
		System.out.println("Trying to connect to " + _sourceName + new Date().toString());
		switch (_deviceType) {
		case PRINTER:
			try {
				AttributeSet att = _printService.getAttributes();
				for (Attribute a : att.toArray()) {
					if(a.getName().equals("printer-is-accepting-jobs")){
						if("accepting-jobs".equalsIgnoreCase(att.get(a.getClass()).toString())){
							status = true;
						}
						System.out.println(a.getName() + " : " + att.get(a.getClass()).toString());
						break;
					}
				}
			} catch (Exception e) {
				System.out.println(e.getMessage());
				e.printStackTrace();
				status = false;
			}
			break;
		default:
			System.out.println("Invalid device type");
			break;
		}
		_lastStatus = status;
		return status;
	}

	@Override
	public String getEmailBody() {
		return "Cannot connect to device : \n\t\t" + _printService.getName() + " [" +  _sourceDescription + " ]" + "\n Time: "+ new Date().toString();
	}

	@Override
	public String getEmailSubject() {
		return _printService.getName() + " Failure " + new Date().toString();
	}


}
